#include "../../Headers/UI/clientsadddialog.h"
#include "ui_clientsadddialog.h"

#include <QTextCodec>

ClientsAddDialog::ClientsAddDialog(MyUtils::FormStatus formStatus, Client *client, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ClientsAddDialog)
{
    ui->setupUi(this);

    this->setWindowFlags(Qt::Dialog | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);

    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));

    this->formStatus = formStatus;

    switch(formStatus)
    {
    case MyUtils::Adding:
        break;
    case MyUtils::Editing:
        ui->createButton->setText(trUtf8("Изменить"));
        if (client != NULL)
        {
            ui->firstNameLineEdit->setText(client->getFirstName());
            ui->lastNameLineEdit->setText(client->getLastName());
            ui->documentLineEdit->setText(client->getDocument());
            ui->phoneLineEdit->setText(client->getPhone());

            this->client = client;
        }
        break;
    }
}

ClientsAddDialog::~ClientsAddDialog()
{
    delete ui;
}

/**
  Создать клиента
  */
void ClientsAddDialog::on_createButton_clicked()
{
    ClientDataAccessor dataAccessor;
    Client client;
    client.setFirstName(ui->firstNameLineEdit->text());
    client.setLastName(ui->lastNameLineEdit->text());
    client.setDocument(ui->documentLineEdit->text());
    client.setPhone(ui->phoneLineEdit->text());

    switch(formStatus)
    {
    case MyUtils::Adding:
        dataAccessor.setClient(client);
        break;
    case MyUtils::Editing:
        client.setId(this->client->getId());
        dataAccessor.editClient(client);
        break;
    }

    this->close();
}

/**
  Отмена
  */
void ClientsAddDialog::on_cancelButton_clicked()
{
    this->close();

}
